<template>
  <div class="login" id="loginBack" ref='vantaRef'>
    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
      <h2 class="title">欢迎登录物联云组态</h2>
      <el-form-item prop="username">
        <el-input v-model="loginForm.username" type="text" auto-complete="off" placeholder="账号">
          <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon" />
        </el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input v-model="loginForm.password" type="password" auto-complete="off" placeholder="密码"
          @keyup.enter.native="handleLogin">
          <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
        </el-input>
      </el-form-item>
      <el-form-item prop="code" v-if="captchaOnOff">
        <el-input v-model="loginForm.code" auto-complete="off" placeholder="验证码" style="width: 63%"
          @keyup.enter.native="handleLogin">
          <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />
        </el-input>
        <div class="login-code">
          <img :src="codeUrl" @click="getCode" class="login-code-img" />
        </div>
      </el-form-item>
      <el-checkbox v-model="loginForm.rememberMe" style="margin:0px 0px 25px 0px;">记住密码</el-checkbox>
      <el-form-item style="width:100%;">
        <el-button :loading="loading" size="medium" type="primary" style="width:100%;"
          @click.native.prevent="handleLogin">
          <span v-if="!loading">登 录</span>
          <span v-else>登 录 中...</span>
        </el-button>
        <div style="float: right;" v-if="register">
          <router-link class="link-type" :to="'/register'">立即注册</router-link>
        </div>
      </el-form-item>
    </el-form>
    <!--  底部  -->
    <div class="el-login-footer">
      <span >Copyright © 2018-2023 物联云组态 All Rights Reserved.</span> <a href="http://81.68.197.219:10071/index"
        target="_blank"></a>
    </div>
    <el-dialog title="手机客户端" :visible.sync="dialogCode" width="20%">
      <vue-qr :logoSrc="imageUrl" :text="qrCodeUrl" :size="350"></vue-qr>
    </el-dialog>
    <el-dialog title="温馨提示" :visible.sync="dialogVisible" width="30%" :close-on-press-escape="false"
      :close-on-click-modal="false">
      <div style="font-size: 18px;text-align: center;">
        <span>在线体验，请扫描二维码获取账号！</span>
        <el-image :src="imageURL" style="height: 500px;" />
      </div>
    </el-dialog>
    <div class="login-we-chat" @click="weChatClick">
      <div class="qr-logo">
        <img :src="weChat" width="24" height="24" />
      </div>
      <div style="padding:15px">
        <el-image style="width: 110px; height: 110px" :src="weChatOne"></el-image>
      </div>
      <div class="qr-text">
        <span>个人微信</span>
      </div>
    </div>
  </div>
</template>

<script>
import { getCodeImg } from "@/api/login";
import Cookies from "js-cookie";
import { encrypt, decrypt } from '@/utils/jsencrypt'
import vueQr from 'vue-qr'
import android from '@/assets/image/android.png'
import weChat from '@/assets/image/weChat.png'
import weChatOne from '@/assets/image/weChat.jpg'
export default {
  name: "Login",
  components: {
    vueQr
  },
  data() {
    return {
      codeUrl: "",
      logoUrl: android,
      weChat:weChat,
      weChatOne:weChatOne,
      loginForm: {
        username: "csdn",
        password: "123456",
        rememberMe: false,
        code: "",
        uuid: "",
      },
      loginRules: {
        username: [
          { required: true, trigger: "blur", message: "请输入您的账号" }
        ],
        password: [
          { required: true, trigger: "blur", message: "请输入您的密码" }
        ],
        code: [{ required: true, trigger: "change", message: "请输入验证码" }]
      },
      loading: false,
      // 验证码开关
      captchaOnOff: false,
      // 注册开关
      register: false,
      redirect: undefined,
      imageUrl: require("@/assets/logo/logo.png"),
      qrCodeUrl: '',
      dialogCode: false,
      vantaEffect: null,
      dialogVisible: false,
      imageURL: 'https://mp-bd83e627-0227-46f3-aef3-95bef99ce553.cdn.bspapp.com/cloudstorage/7b0abe90-ec82-429d-b8fa-6a51fd610c60.jpg',
    };
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true
    }
  },
  created() {
    this.getCode();
    this.getCookie();
  },
  mounted() {
  },
  beforeDestroy() {
    if (this.vantaEffect) {
      this.vantaEffect.destroy()
    }
  },
  methods: {
    weChatClick(){
      this.dialogVisible=true;
    },
    codeClick() {
      this.dialogCode = true;
    },
    getCode() {
      getCodeImg().then(res => {
        this.captchaOnOff = res.captchaOnOff === undefined ? true : res.captchaOnOff;
        if (this.captchaOnOff) {
          this.codeUrl = "data:image/gif;base64," + res.img;
          this.loginForm.uuid = res.uuid;
        }
      });
    },
    getCookie() {
      const username = Cookies.get("username");
      const password = Cookies.get("password");
      const rememberMe = Cookies.get('rememberMe')
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password: password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
      };
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true;
          if (this.loginForm.rememberMe) {
            Cookies.set("username", this.loginForm.username, { expires: 30 });
            Cookies.set("password", encrypt(this.loginForm.password), { expires: 30 });
            Cookies.set('rememberMe', this.loginForm.rememberMe, { expires: 30 });
          } else {
            Cookies.remove("username");
            Cookies.remove("password");
            Cookies.remove('rememberMe');
          }
          this.$store.dispatch("Login", this.loginForm).then(() => {
            this.$router.push({ path: this.redirect || "/" }).catch(() => { });
          }).catch(() => {
            this.loading = false;
            if (this.captchaOnOff) {
              this.getCode();
            }
          });
        }
      });
    }
  }
};
</script>

<style rel="stylesheet/scss" lang="scss">
.login {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  background-image: url("../assets/image/login.png");
  background-size: cover;
}

.title {
  margin: 0px auto 30px auto;
  text-align: center;

  font-family: Source Han Sans CN;
  font-size: 23px;
  font-weight: 700;
  background: linear-gradient(315deg, #c805bb 3%, #49df5d 38%, #30eee2 68%, #ff1919 98%);
  -webkit-animation: title-data 8s ease infinite;
  animation: title-data 8s ease infinite;
  background-size: 400% 400%;
  color: transparent;
  -webkit-box-reflect: below 2px linear-gradient(transparent, rgba(0, 0, 0, .5));
}

@keyframes title-data {
  0% {
    background-position: 0 0;
    -webkit-background-clip: text;
  }

  50% {
    background-position: 100% 100%;
    -webkit-background-clip: text;
  }

  100% {
    background-position: 0 0;
    -webkit-background-clip: text;
  }
}
.login-form {
  position: fixed;
  top: 25%;
  right: 16%;
  border-radius: 6px;
  background: #ffffff;
  width: 400px;
  padding: 25px 25px 5px 25px;
  border: solid 1px #cbced1;
  box-shadow: 5px 5px 8px 5px #d3d1d1;//X轴、Y轴、模糊距离、阴影范围

  .el-input {
    height: 38px;

    input {
      height: 38px;
    }
  }

  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 2px;
  }
}

.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}

.login-code {
  width: 33%;
  height: 38px;
  float: right;

  img {
    cursor: pointer;
    vertical-align: middle;
  }
}

.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #000;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}

.login-code-img {
  height: 38px;
}

.login-qr-code {
  position: absolute;
  width: 150px;
  height: 175px;
  padding: 10px;
  color: #999;
  z-index: 100;
  right: 20px;
  top: 20px;
  text-align: center;
  background-color: #fff;
  border: 1px solid #ccc;
  border: 1px solid rgba(0, 0, 0, .15);
  -webkit-box-shadow: 0 6px 12px rgb(0 0 0 / 18%);
  box-shadow: 0 6px 12px rgb(0 0 0 / 18%);
  background-clip: padding-box;
  border-radius: 3px 0 3px 3px;
  -webkit-transition: left .3s ease-in-out .1s;
  transition: left .3s ease-in-out .1s;
}

.qr-text {
  font-weight: bolder;
  margin-bottom: 0;
  font-size: 15px;
}

.qr-logo {
  position: absolute;
  top: -15px;
}
.login-we-chat{
  position: absolute;
  width: 150px;
  height: 185px;
  padding: 10px;
  color: #999;
  z-index: 100;
  right: 10px;
  top: 10px;
  text-align: center;
  background-color: #fff;
  border: 1px solid #ccc;
  border: 1px solid rgba(0, 0, 0, .15);
  -webkit-box-shadow: 0 6px 12px rgb(0 0 0 / 18%);
  box-shadow: 0 6px 12px rgb(0 0 0 / 18%);
  background-clip: padding-box;
  border-radius: 3px 0 3px 3px;
  -webkit-transition: left .3s ease-in-out .1s;
  transition: left .3s ease-in-out .1s;
}
</style>
